<?php

namespace addons\Calendar\merchant\controllers;

use common\helpers\ArrayHelper;
use Yii;
use yii\db\Expression;
use common\traits\Curd;
use common\models\base\SearchModel;
use common\enums\StatusEnum;
use addons\Calendar\common\models\Calendar;


class DefaultController extends BaseController
{
    use Curd;


    public $modelClass = Calendar::class;


    /**
     * 首页
     *
     * @return string
     * @throws \yii\web\NotFoundHttpException
     */
    public function actionIndex()
    {
        $searchModel = new SearchModel([
            'model' => $this->modelClass,
            'scenario' => 'default',
            'relations' => ['member' => ['realname']], // 关联 member 表的 nickname 字段
            'partialMatchAttributes' => ['title', 'member.realname'], // 模糊查询，注意 member_nickname 为关联表的别名 表名_字段
            'defaultOrder' => [
                'id' => SORT_DESC
            ],
            'pageSize' => $this->pageSize
        ]);
        $where = new Expression("FIND_IN_SET(:supplier, attend)", [":supplier" => Yii::$app->user->id]);
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $dataProvider->query
            ->andWhere(['or', $where, [$this->modelClass::tableName().'.member_id' => Yii::$app->user->id]])
            ->andFilterWhere([$this->modelClass::tableName().'.merchant_id' => $this->getMerchantId()]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
        ]);
    }

    public function actionCreate()
    {
        $id = Yii::$app->request->get('id', null);
        $model = $this->findModel($id);
        // ajax 校验
        $this->activeFormValidate($model);
        if ($model->load(Yii::$app->request->post())) {

            return $model->save()
                ? $this->redirect(['index'])
                : $this->message($this->getError($model), $this->redirect(['index']), 'error');
        }
        return $this->renderAjax('create', [
            'model' => $model,
            'data' => Yii::$app->services->merchantMember->getMap(),

        ]);
    }
    public function actionEdit()
    {
        $id = Yii::$app->request->get('id', null);
        $model = $this->findModel($id);
        // ajax 校验
        $this->activeFormValidate($model);
        if ($model->load(Yii::$app->request->post())) {

            return $model->save()
                ? $this->redirect(['index'])
                : $this->message($this->getError($model), $this->redirect(['index']), 'error');
        }
        return $this->renderAjax('edit', [
            'model' => $model,
            'data' => Yii::$app->services->merchantMember->getMap(),

        ]);
    }
    /**
     * 详情
     *
     * @param $id
     * @return mixed|string
     */
    public function actionView($id)
    {
//        $this->layout = "@addons/Attend/backend/views/layouts/main";

        if (empty($id) || empty(($model = $this->modelClass::find()->where([
                'id' => $id,
                'status' => StatusEnum::ENABLED
            ])->one()))) {
            return $this->message('找不到该文章', $this->redirect(['index']), 'error');
        }

        return $this->render('view', [
            'model' => $model,
        ]);
    }

    /**
     * 还原
     *
     * @param $id
     * @return mixed
     */
    public function actionShow($id)
    {
        $model = $this->findModel($id);
        $model->status = StatusEnum::ENABLED;
        if ($model->save()) {
            return $this->message("还原成功", $this->redirect(['recycle']));
        }

        return $this->message("还原失败", $this->redirect(['recycle']), 'error');
    }

    /**
     * 伪删除
     *
     * @param $id
     * @return mixed
     */
    public function actionHide($id)
    {
        $model = $this->findModel($id);
        if ($model->save()) {
            return $this->message("删除成功", $this->redirect(['index']));
        }

        return $this->message("删除失败", $this->redirect(['index']), 'error');
    }

    /**
     * 删除
     *
     * @param $id
     * @return mixed
     * @throws \Throwable
     * @throws \yii\db\StaleObjectException
     */
    public function actionDelete($id)
    {
        if ($this->findModel($id)->delete()) {
            return $this->message("删除成功", $this->redirect(['index']));
        }

        return $this->message("删除失败", $this->redirect(['index']), 'error');
    }

    /**
     * 回收站
     *
     * @return mixed
     */
    public function actionRecycle()
    {
        $searchModel = new SearchModel([
            'model' => $this->modelClass,
            'scenario' => 'default',
            'partialMatchAttributes' => ['title'], // 模糊查询
            'defaultOrder' => [
                'sort' => SORT_ASC,
                'id' => SORT_DESC
            ],
            'pageSize' => $this->pageSize
        ]);

        $dataProvider = $searchModel
            ->search(Yii::$app->request->queryParams);
        $dataProvider->query
            ->andWhere(['<', 'status', StatusEnum::DISABLED]);
        return $this->render('recycle', [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,

        ]);
    }
    protected function findModel($id)
    {
        /* @var $model \yii\db\ActiveRecord */
        if (empty($id) || empty(($model = $this->modelClass::findOne(['id' => $id, 'merchant_id' => $this->getMerchantId()])))) {

            $model = new $this->modelClass;
            return $model->loadDefaultValues();
        }

        return $model;
    }

}
